﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Ex03.GarageLogic
{
    class InternalCombustionPropulsion
    {
        private const float k_MinimalFuelAmountInFuelTank = 0.0F;

        private EnergyType.eEnergyType m_FuelType = EnergyType.eEnergyType.DefaultValue;
        private float m_CurrentFuelAmountInTankInLitres = 0.0F;
        private float m_MaximumFuelAmountInTankInLitres = 0.0F;

        public InternalCombustionPropulsion() : this(EnergyType.eEnergyType.DefaultValue, k_MinimalFuelAmountInFuelTank) {}
        
        public InternalCombustionPropulsion(
                EnergyType.eEnergyType i_PropulsionFuelType, 
                float i_MaximumFuelAmountInTankInLitres)
        {
            if (checkIsFuelTypeLegal(i_PropulsionFuelType) && 
                checkIsMaximumFuelAmountValueLegal(i_MaximumFuelAmountInTankInLitres))
            {
                m_MaximumFuelAmountInTankInLitres = i_MaximumFuelAmountInTankInLitres;
                m_FuelType = i_PropulsionFuelType;
            }
        }

        private bool checkIsFuelTypeLegal(EnergyType.eEnergyType i_PropulsionFuelType)
        {
            bool ReturnValue = false;

            switch (i_PropulsionFuelType)
            {
                case EnergyType.eEnergyType.Octan95:
                case EnergyType.eEnergyType.Octan96:
                case EnergyType.eEnergyType.Octan98:
                case EnergyType.eEnergyType.Soler:
                    ReturnValue = true;
                    break;
                case EnergyType.eEnergyType.NiMHBattery:
                case EnergyType.eEnergyType.DefaultValue:
                default:
                    break;
            }

            if (ReturnValue == false)
            {
                ArgumentException FuelTypeError = new ArgumentException();

                throw FuelTypeError;
            }
            
            return ReturnValue;
        }

        private bool checkIsMaximumFuelAmountValueLegal(float i_MaximumFuelAmountInTankInLitres)
        {
            bool ReturnValue = false;

            if (i_MaximumFuelAmountInTankInLitres >= k_MinimalFuelAmountInFuelTank)
            {
                ReturnValue = true;
            }
            else
            {
                ValueOutOfRangeException FuelTankSizeError = new ValueOutOfRangeException();

                throw FuelTankSizeError;
            }
            
            return ReturnValue;
        }
        
        private bool doesFuelAmountLegalForFuelTank(float i_FuelAmountInput)
        {
            bool ReturnValue = false;

            if ((i_FuelAmountInput <= MaximumFuelAmountInTankInLitres) &&
                (i_FuelAmountInput >= k_MinimalFuelAmountInFuelTank))
            {
                ReturnValue = true;
            }
            else
            {
                ValueOutOfRangeException FuelAmountInputException = new ValueOutOfRangeException();

                throw FuelAmountInputException;
            }

            return ReturnValue;
        }

        public static bool DoesFuelTypesMatch(EnergyType.eEnergyType i_FirstFuelType,EnergyType.eEnergyType i_SecondFuelType)
        {
 	        bool ReturnValue = false;

            if (i_SecondFuelType == i_FirstFuelType)
            {
                ReturnValue = true;
            }

            return ReturnValue;
        }

        private bool wouldTankOverflow(float i_FuelAmountToRefuel)
        {
            return (i_FuelAmountToRefuel + m_CurrentFuelAmountInTankInLitres > m_MaximumFuelAmountInTankInLitres);
        }

        public EnergyType.eEnergyType FuelType
        {
            get { return m_FuelType; }
            set
            {
                if (checkIsFuelTypeLegal(value))
                {
                    m_FuelType = value;
                }
            }
        }

        public float CurrentFuelAmountInTank
        {
            get { return m_CurrentFuelAmountInTankInLitres; }
            set
            {
                if (doesFuelAmountLegalForFuelTank(value))
                {
                    m_CurrentFuelAmountInTankInLitres = value;
                }
            }
        }

        public float MaximumFuelAmountInTankInLitres
        {
            get { return m_MaximumFuelAmountInTankInLitres; }
            set
            {
                if (checkIsMaximumFuelAmountValueLegal(value))
                {
                    m_MaximumFuelAmountInTankInLitres = value;
                }
            }
        }

        public bool AddPropulsionMaterial(
            EnergyType.eEnergyType i_PropulsionMaterialType, float i_PropulsionMaterialAmountToAdd)
        {
            bool ReturnValue = false;

            if (checkIsFuelTypeLegal(i_PropulsionMaterialType) && 
                DoesFuelTypesMatch(i_PropulsionMaterialType, m_FuelType) && 
                wouldTankOverflow(i_PropulsionMaterialAmountToAdd))
            {
                m_CurrentFuelAmountInTankInLitres += i_PropulsionMaterialAmountToAdd;
                ReturnValue = true;
            }

            return ReturnValue;
        }

        /*
        //engine functions.
        public FuelType.eFuelType PropulsionFuelType
        {
            get { return m_Engine.PropulsionFuelType; }
       //     set { m_Engine.PropulsionFuelType = value; }
        }

        public float CurrentFuelAmountInTankInLitres
        {
            get { return m_Engine.CurrentFuelAmountInTankInLitres; }
            set { m_Engine.CurrentFuelAmountInTankInLitres = value; }
        }

        public float MaximumFuelAmountInTankInLitres
        {
            get { return m_Engine.MaximumFuelAmountInTankInLitres; }
         //   set { m_Engine.MaximumFuelAmountInTankInLitres = value; }
        }

        public bool RefuelVehicle(float i_FuelAmountToRefuel, FuelType.eFuelType i_FuelType)
        {
            return m_Engine.RefuelVehicle(i_FuelAmountToRefuel, i_FuelType);
        }
        //engine functions end.*/
    }
}
